package com.runa.rccprotocol.analysis.hanyang;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.persistence.DateUtil;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.UnitUtil;
import com.runa.rccprotocol.analysis.wukexing.WkxAnalysis;
import com.runa.rccprotocol.dto.runa.RunaConcenRequestData;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import com.runa.rccprotocol.dto.runa.RunaICStruct;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.runa.rccprotocol.dto.runa.RunaFrameSequence.fn;
import static com.runa.rccprotocol.dto.runa.RunaFrameSequence.pn;

/**
 * 2024/11/28 14:17
 * zhangYB
 */
@Data
public class HyAnalysis {

    private IInterData interData;

    public int handleRunaDataForward(RunaICStruct struct, byte[] data, int offset) {
        int pn = pn(data[offset], data[offset+1]);
        int fn = fn(data[offset+2], data[offset+3]);
        if (pn == 0 && fn == 1) {
            int len = data[offset+5] + (data[offset+6]<<8);
            byte[] bs = new byte[len];
            System.arraycopy(data, offset+7, bs, 0, bs.length);
            handleData(bs, 0, bs.length);
            if (interData instanceof RunaConcenResponseData) {
                RunaConcenResponseData responseData = (RunaConcenResponseData)interData;
                responseData.setAddress(struct.getArea().toString());
                responseData.setCommand(struct.getAfn());
            }
        }
        return 0;
    }

    public int handleData(byte[] bs, int begin, int length) {
        for (int i = 0; i < length; i++) {
            if ((bs[i] & 0xff) == 0xFE) {
                continue;
            }
            if ((bs[i] & 0xff) == 0x68 && (bs[i+1] & 0xff) == 0x20) {
                return meterHandle(bs, i, length);
            }
        }
        return 0;
    }


    private int meterHandle(byte[] bs, int begin, int length) {
        if (begin + 2 + 7 + 2 > length) {
            return -1;
        }
        int len = bs[begin+10];
        if (begin + 2 + 7 + 2 + len + 2 > length) {
            return -1;
        }

        if (bs[begin + 2 + 7 + 2 + len] != ByteUtil.sum(bs, begin, 2 + 7 + 2 + len)) {
            return 0;
        }

        int c = bs[begin+9] & 0xff;
        switch(c) {
            case 0x81:
                return readMeterResponse(bs, begin, length);
        }

        return 0;
    }


    private int readMeterResponse(byte[] bs, int begin, int length) {
        HotMeterVO hotMeterVO = new HotMeterVO();
        hotMeterVO.setReadDate(new Date());
        begin += 2;
        String serial = ByteUtil.byteToHexStringFlip(bs, begin, 4);
        hotMeterVO.setMeterSerial(serial);
        begin = begin + 12;

        float accCold = (float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 4))) * 0.01);
        begin += 4;
        accCold = UnitUtil.toKwh(accCold, bs[begin++] & 0xff);

        float accHeat = (float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 4))) * 0.01);
        begin += 4;
        hotMeterVO.setAccHeat(UnitUtil.toKwh(accHeat, bs[begin++] & 0xff));

        float power = (float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 4))) * 0.01);
        begin += 4;
        hotMeterVO.setPower(UnitUtil.toKw(power, bs[begin++] & 0xff));

        float flow = (float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 4))) * 0.0001);
        begin += 4;
        hotMeterVO.setFlow(UnitUtil.toM3h(flow, bs[begin++] & 0xff));

        float accFlow = (float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 4))) * 0.01);
        begin += 4;
        hotMeterVO.setAccFlow(UnitUtil.toM3(accFlow, bs[begin++] & 0xff));

        hotMeterVO.setSupplyTemp((float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 3))) * 0.01));
        begin += 3;

        hotMeterVO.setReturnTemp((float)((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 3))) * 0.01));
        begin += 3;

        hotMeterVO.setWorkTime((Integer.valueOf(ByteUtil.byteToHexStringFlip(bs, begin, 3))));
        begin += 3;

        hotMeterVO.setMeterDate(DateUtil.stringToDate(ByteUtil.byteToHexStringFlip(bs, begin, 7), DateUtil.NO_LINE_FORMAT));
        begin += 7;

        hotMeterVO.setStatus(ByteUtil.byteToHexString(bs, begin, 2));
        byte s1 = bs[begin++];
        byte s2 = bs[begin++];
        List<String> faultList = new ArrayList<>();
        if ((s1 >> 2 & 1) == 1) {
            faultList.add("电池欠压");
        }
        if ((s2 >> 1 & 1) == 1) {
            faultList.add("进水故障");
        }
        if ((s2 >> 2 & 1) == 1) {
            faultList.add("回水故障");
        }
        hotMeterVO.setFaultCode(faultList);
        RunaConcenResponseData runaConcenResponseData = new RunaConcenResponseData();
        runaConcenResponseData.addRepData(hotMeterVO);
        this.interData = runaConcenResponseData;
        return 0;
    }



    public static void main(String[] args) {
        String s = "FE FE FE 68 20 78 56 34 12 00 35 79 81 2E 1F 90 12 13 57 90 24 05 12 56 35 47 05 23 57 64 83 17 41 62 51 74 35 70 43 61 32 2C 14 15 00 62 14 00 49 01 00 30 54 13 17 10 12 20 04 00 2A 16 ";
        byte[] bytes = ByteUtil.hexStringToByte(s);
        HyAnalysis hyAnalysis = new HyAnalysis();
        hyAnalysis.handleData(bytes, 0, bytes.length);
        System.out.println(JSON.toJSONString(hyAnalysis.getInterData()));


        RunaConcenRequestData requestData = new RunaConcenRequestData("");
        requestData.readHyMeter("", 1);


    }


}
